package com.example.shop.modules.manage.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shop.modules.manage.service.IIndexSalesadminService;
import com.example.shop.persistence.entity.Goods;
import com.example.shop.persistence.entity.IndexSales;
import com.example.shop.persistence.mapper.GoodsMapper;
import com.example.shop.persistence.mapper.IndexSalesMapper;
import com.example.shop.utils.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.example.shop.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 首页商品促销分类表 服务实现类
 * </p>
 *
 * @author jason
 * @since 2020-06-15
 */
@Service
public class IndexSalesAdminServiceImpl extends ServiceImpl<IndexSalesMapper, IndexSales> implements IIndexSalesadminService {

    @Autowired
    private IndexSalesMapper indexSalesMapper;
    
    @Autowired
    private GoodsMapper goodsMapper;

    // 查询
    public JSONResult selectSales(){

        // 先查询出数据
        List<IndexSales> list = indexSalesMapper.selectList(null);
        // 循环
        for (IndexSales indexSales: list){
            // 根据商品ID 查询商品数据
            String ids = indexSales.getGoodsIDS();
            if(StrUtil.isNotEmpty(ids)){
                String[] strs = ids.split(",");
                List<Goods> goodsList = new ArrayList<Goods>();
                for (String id: strs){
                    Goods goods = goodsMapper.selectById(id);
                    if(goods != null){
                        goodsList.add(goods);
                    }
                }
                indexSales.setGoodsList(goodsList);
            }
        }
        return renderSuccess(list);
    }
}
